package com.lfy.fyadmin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lfy.fyadmin.common.R;
import com.lfy.fyadmin.entity.Permission;
import com.lfy.fyadmin.service.PermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/permission")
@Tag(name = "权限管理", description = "权限相关接口")
public class PermissionController {

    private final PermissionService permissionService;

    public PermissionController(PermissionService permissionService) {
        this.permissionService = permissionService;
    }

    @PostMapping
    @Operation(summary = "新增权限")
    public R<Boolean> save(@RequestBody Permission permission) {
        boolean result = permissionService.save(permission);
        return result ? R.ok(true) : R.fail("新增失败");
    }

    @PutMapping
    @Operation(summary = "修改权限")
    public R<Boolean> update(@RequestBody Permission permission) {
        boolean result = permissionService.updateSelective(permission);
        return result ? R.ok(true) : R.fail("修改失败");
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除权限")
    public R<Boolean> delete(@PathVariable Integer id) {
        boolean result = permissionService.removeById(id);
        return result ? R.ok(true) : R.fail("删除失败");
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询权限")
    public R<Permission> getById(@PathVariable Integer id) {
        Permission permission = permissionService.getById(id);
        return permission != null ? R.ok(permission) : R.fail("未找到权限");
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询权限")
    public R<IPage<Permission>> findPage(@RequestParam Integer pageNum,
                                         @RequestParam Integer pageSize) {
        Page<Permission> page = new Page<>(pageNum, pageSize);
        return R.ok(permissionService.page(page));
    }
}